﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastEF.Base;
using Newtonsoft.Json.Linq;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;

namespace FastEF.Publish
{
    public class DBUtil
    {
     
        public DBUtil()
        {
            
        }
        /// <summary>
        /// 初始化所有EF数据库对象上下文，避免创建每个类型的dbcontext时都会进行内存映射消耗时间，该方法应用于启动项目文件中
        /// </summary>
        public void InitEF()
        {
            using (var dbcontext = new BaseDbContext<BaseModel>())
            {
                var objectContext = ((IObjectContextAdapter)dbcontext).ObjectContext;
                var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
                mappingCollection.GenerateViews(new List<EdmSchemaError>());
            }

        }
        public T FindOne<T>(string KeyGuid) where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            return context.FindOne(KeyGuid);
        }
        public List<T> FindList<T>(string SQL) where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            return context.FindList(SQL);
        }
        public List<T> FindListPage<T>(string SQL, int PageNumber, int PageSize, string OrderBy) where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            return context.FindListPage(SQL, PageNumber, PageSize, OrderBy);
        }
        public void Insert<T>(T Entity) where T:TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            context.Insert(Entity);
        }
        public void InsertBatch<T>(List<T> EntityList) where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            context.InsertBatch(EntityList);
        }
        public void Save<T>(T Entity) where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            context.Save(Entity);
        }
        public void Remove<T>(T Entity) where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            context.Remove(Entity);
        }
        public int Count<T>() where T : TModel
        {
            BaseDbContext<T> context = new BaseDbContext<T>();
            return context.Count<T>();
        }
        public List<JObject> FindView(string SQL)
        {
            BaseDbContext<BaseModel> context = new BaseDbContext<BaseModel>();
            return context.FindView(SQL);
        }
        public void ExecuteSql(string SQL)
        {
            BaseDbContext<BaseModel> context = new BaseDbContext<BaseModel>();
            context.ExecuteSql(SQL);
        }
        public int ExecuteSqlToInt(string SQL)
        {
            BaseDbContext<BaseModel> context = new BaseDbContext<BaseModel>();
            return context.ExecuteSqlToInt(SQL);
        }
        public string ExecuteSqlToString(string SQL)
        {
            BaseDbContext<BaseModel> context = new BaseDbContext<BaseModel>();
            return context.ExecuteSqlToString(SQL);
        }
        public void ExecuteTransition(Action dataBaseAction, out string exceptionStr)
        {
            BaseDbContext<BaseModel> context = new BaseDbContext<BaseModel>();
            exceptionStr = string.Empty;
            var tran = context.Database.BeginTransaction();  //开启事务
            try
            {
                dataBaseAction();
                tran.Commit();  //必须调用Commit()，不然数据不会保存
            }
            catch (Exception ex)
            {
                exceptionStr = ex.Message;
                tran.Rollback();    //出错就回滚
            }

        }


      
    }
}
